package com.xmwzw.catpu;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xmwzw.catpu.databinding.ActivityHomeBinding;
import com.xmwzw.catpu.databinding.DialogLinkBinding;
import com.xmwzw.catpu.http.bean.TypeBean;
import com.xmwzw.catpu.util.Constant;
import com.xmwzw.catpu.widget.SimpleDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : wangzw
 * @date : 20-3-31上午10:13
 * @desc :
 */
public class HomeFragment extends BaseFragment {
    private ActivityHomeBinding homeBinding;
    private SimpleDialog simpleDialog;
    private List<Fragment> fragments = new ArrayList<>();


    @Override
    public int getContentLayout() {
        return R.layout.activity_home;
    }

    @Override
    public void findViews(View view) {
        homeBinding = getBaseViewBinding();
        homeBinding.setOnClick(new OnClick());
        addView();

    }

    private void addView() {
        for (int i = 0; i < Constant.typeBeans.size(); i++) {
            TypeBean typeBean = Constant.typeBeans.get(i);
            RadioButton radioButton = (RadioButton) LayoutInflater.from(getActivity())
                    .inflate(R.layout.view_type, null);
            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            radioButton.setLayoutParams(params);
            radioButton.setText(typeBean.getName());
            radioButton.setTag(typeBean.getId());
            radioButton.setId(i);
            homeBinding.rg.addView(radioButton);

            if (homeBinding.rg.getChildAt(0)!=null){
                ((RadioButton) homeBinding.rg.getChildAt(0)).setChecked(true);
            }

            fragments.add(newInstance((Integer.valueOf(typeBean.getId()))));

        }
        homeBinding.viewpager.setAdapter(new MyPageAdapter(getChildFragmentManager(), fragments));
        homeBinding.viewpager.setCurrentItem(0);
        homeBinding.viewpager.setNoScroll(true);

        homeBinding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                homeBinding.viewpager.setCurrentItem(i,false);

            }
        });

    }

    //传输数据
    public TypeListFragment newInstance(int type) {
        TypeListFragment f = new TypeListFragment();
        Bundle b = new Bundle();
        b.putInt("type", type);
        f.setArguments(b);
        return f;
    }

    public static class MyPageAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;

        public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }

    public class OnClick {
        public void search(){
            Intent intent = new Intent(getActivity(),SearchResultActivity.class);
            startActivity(intent);

        }
        public void share() {

            View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_link, null, false);
            final DialogLinkBinding dialogLinkBinding = DataBindingUtil.bind(view);
            simpleDialog = new SimpleDialog(getActivity(), view, R.style.DialogTheme);
            simpleDialog.setCancelable(true);
            simpleDialog.show();
            dialogLinkBinding.btnCopy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //获取剪贴板管理器：
                    ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

                    ClipData mClipData = ClipData.newPlainText("Label", dialogLinkBinding.tvContent.getText().toString());

                    cm.setPrimaryClip(mClipData);

                    ((BaseActivity) getActivity()).showToast("已复制到剪贴板");

                    if (simpleDialog != null) {
                        simpleDialog.cancel();
                        simpleDialog.dismiss();
                    }

                }
            });

            dialogLinkBinding.ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (simpleDialog != null) {
                        simpleDialog.cancel();
                        simpleDialog.dismiss();
                    }
                }
            });

        }
    }


}
